<?php
namespace app\store\controller\content;
use app\store\controller\Controller;
use app\store\model\Notice as NoticeModel;
/**
 * 公告控制器
 */
class Notice extends Controller{
    /**
     * 公告列表
     */
    public function index(){
        $model=new NoticeModel;
        $list=$model->getList($this->getWxappId());
        return $this->fetch('index', compact('list'));
    }
    /**
     * 添加公告
     */
    public function add(){
        $model=new NoticeModel;
        if (!$this->request->isAjax()){
            return $this->fetch('add');
        }
        //新增记录
        if ($model->add($this->postData('article'))){
            return $this->renderSuccess('添加成功', url('content.notice/index'));
        }
        return $this->renderError($model->getError()?:'添加失败');
    }
    /**
     * 更新公告
     */
    public function edit($id){
        //公告详情
        $model=NoticeModel::detail($id);
        if (!$this->request->isAjax()){
            return $this->fetch('edit',['model'=>$model]);
        }
        //更新记录
        if ($model->edit($this->postData('article'))){
            return $this->renderSuccess('更新成功', url('content.notice/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
    /**
     * 删除公告
     */
    public function delete($id){
        //公告详情
        $model=NoticeModel::detail($id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}